<template>
    <div class="mainDiv" style="padding: 20px;box-sizing: border-box;color:#000">
        <div style="height: 100%">
        <div style="display: flex;flex-direction: column" v-for="m in modellist" v-if="m.category===1" :key="m.id">
            <h3 style="margin: 10px 0px">{{m.name}}</h3>
            <div>
                <el-checkbox v-model="d.checkes" v-for="(d, k) in modellist" v-if="d.category===2&&d.pid===m.id" :key="d.id">
                    {{d.name}}
                </el-checkbox>
            </div>
        </div>
        </div>
        <div style="text-align: right;">
            <el-button type="danger" size="mini" @click="close">取消</el-button>
            <el-button type="primary" size="mini" @click="callSubmit">确定</el-button>
        </div>
    </div>
</template>
<script type="text/javascript">
import C from '#/src/common/common.js'
export default {
    name: 'ruleid',
    data() {
        return {
            modellist: [],
            submodel: {},
            modelname: [],
        }
    },
    mounted() {
        this.getLIst()
    },
    methods: {
        getLIst() {
            let that = this
            C.ajax('/organization/rule/editrule', { id: C.getDatas().id, grouptype: C.getDatas().grouptype }).then(function(data) {
                that.submodel = JSON.parse(data.usermodellist) || []
                data.mlist = data.mlist.map(row => {
                    row.checkes = false
                    for (let i in that.submodel) {
                        if (row.tags === i) {
                            row.checkes = that.submodel[i]
                        }
                    }
                    return row
                })
                that.modellist = data.mlist
            })
        },
        async callSubmit(index) {
            // 筛选被选中选项
            let submodel = {}
            this.modellist.forEach(row => {
                if (row.checkes) {
                    submodel[row.tags] = true
                }
            })

            var data = {
                groupid: C.getDatas().id,
                grouptype: C.getDatas().grouptype,
                modelarr: JSON.stringify(submodel),
            }
            C.confirm('确认修改？', async() => {
                let res = await C.ajax('/organization/rule/editrule', data, 'post')
                if (res) C.closeBrowserWindow(res.data)
            })
        },
        close() {
            C.closeBrowserWindow()
        }
    }
}
// const request = C.getDatas()
</script>